package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"meituan-waimai/models/mysql"
	"strings"

	"meituan-waimai/meituanrpc/internal/svc"
	"meituan-waimai/meituanrpc/meituanrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderEvaluateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderEvaluateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderEvaluateLogic {
	return &OrderEvaluateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderEvaluateLogic) OrderEvaluate(in *meituanrpc.OrderEvaluateRequest) (*meituanrpc.OrderEvaluateResponse, error) {
	// todo: add your logic here and delete this line
	var orderEva mysql.OrderEvaluate
	mysql.DB.Where("user_id = ? AND order_id = ?", in.UserId, in.OrderId).First(&orderEva)
	if orderEva.ID != 0 {
		return nil, fmt.Errorf("此订单您已经评价过了，请勿重复评价")
	}
	if strings.Contains(in.Content, "傻子") || strings.Contains(in.Content, "傻逼") {
		return nil, fmt.Errorf("评价内容违规")
	}
	orderEva = mysql.OrderEvaluate{
		Model:   gorm.Model{},
		UserId:  in.UserId,
		OrderId: in.OrderId,
		Content: in.Content,
		Star:    int(in.Star),
		Target:  int(in.Target),
	}
	mysql.DB.Create(&orderEva)
	return &meituanrpc.OrderEvaluateResponse{}, nil
}
